import Taro from '@tarojs/taro';

export const mustLoginCode = ['10020001', '120002', '120003', '120010', '10020002', '10020302', '10020301'];
const EXCLUDE_RELAUNCH_PAGE = ['pages/loginGuide/index', 'pages/login/index'];
// 提示用户中间件
export const remindUserMiddleware = (result, next) => {
  const { errorCode, errorMsg } = result.data;
  if (errorMsg) {
    const currentPage: any = Taro.getCurrentPages().pop();
    if (!EXCLUDE_RELAUNCH_PAGE.includes(currentPage?.route) && mustLoginCode.includes(String(errorCode))) {
      Taro.showToast({
        title: '登录失效，请重新登录',
        icon: 'none',
        mask: true,
        duration: 2000,
      });
      setTimeout(() => {
        Taro.reLaunch({ url: '/pages/loginGuide/index' });
      }, 2000);
    }
  }
  next(result);
};
